<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 15:53
 */

namespace app\index\server;


use app\index\model\Category as CategoryModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class CategoryServer extends BaseServer
{
    public function getList($params = [])
    {
        $params = $this->prep_param($params);
        try {
            // 总条数
            $count = CategoryModel::count();
            $list = CategoryModel::with(['img'])
                ->order($params['sort'], $params['order'])
                ->limit($params['offset'], $params['limit'])
                ->select();
        } catch (Exception $e) {
            // 有些问题 应该抛出错误异常
           throw new DatabaseException([
               'msg' => $e->getMessage()
           ]);
        }
        return bootstrap_table_return($count, $list);
    }

    public function addOrUpdate($params = [])
    {
        // 处理上传图片
        $upload_img = $this->handle_image($params['alias'], $params['url'], 'category', isset($params['primary_key']));
        if($upload_img['status'] == 0) {
            return setMsg(0, $upload_img['msg']);
        }
        $prep_data = [
            'name' => $params['name'],
            'description' => $params['description'],
            'topic_img_id' => $upload_img['img_id'],
            'status' => $params['status']
        ];
        try {
            if(isset($params['primary_key'])) {
                // 更新
                $result = CategoryModel::where('id', $params['primary_key'])
                    ->update($prep_data);
            } else {
                // 新增
                $result = CategoryModel::insert($prep_data);
            }
            if(!$result) {
                return setMsg(0, '分类操作失败');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '分类操作成功');
    }

    public function detail($id)
    {
        try {
            $result = CategoryModel::with(['img'])
                ->where('id', '=', $id)
                ->find();
            if(!$result) {
                return setMsg(0, '未找到此信息');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, 'OK', $result);
    }

    public function changeStatus($id)
    {
        try {
            $info = CategoryModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此分类状态失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此分类 状态成功');
    }
}